package com.xzy.Controller;

import com.xzy.Service.LoginUserService;
import com.xzy.pojo.LoginUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginUserController {
    @Autowired
    LoginUserService loginUserService;
    @Autowired
    StarsController starsController;
    @ApiOperation("用户进行登录，需用户名，电话[唯一]，密码")
    @PostMapping( "/user/login")
    public String login(LoginUser loginUser, Model model, HttpSession session){
        System.out.println("登录校验");
        try{
            LoginUser loginUser1 = loginUserService.findByNameAndPhone(loginUser.getName(), loginUser.getPhone());
            if(loginUser1!=null){
                if(loginUser.toString().equals(loginUser1.toString())){
                    session.setAttribute("nowUser",loginUser.getName());
                    return "index";
                }else {
                    model.addAttribute("LoginMsg","账号错误");
                    return "login";
                }
            }else {
                model.addAttribute("LoginMsg","账号错误");
                return "login";
            }
        }catch (Exception e){
            model.addAttribute("LoginMsg","账号错误");
            return "login";
        }

    }

    @GetMapping("/")
    @ApiOperation("登录页面，无需参数")
    public String login1(){
        System.out.println("登录");
        return "login";
    }

    @GetMapping("/user/toLogin")
    @ApiOperation("登录页面，支持国际化，超链接使用带国家参数")
    public String login1(String l){
        System.out.println("跳转页面");
        return "login";
    }

    @ApiOperation("注册页面，无需参数")
    @GetMapping("/user/toRegister/1")
    public String toRegister(){
        System.out.println("进入注册账号页面");
        return "register";
    }

    @ApiOperation("注册页面，需用户实体类")
    @GetMapping("/user/toRegister/2")
    public String toRegister(String l){
        System.out.println("进入注册账号页面");
        return "register";
    }

    @ApiOperation("用户注册校验，需用户名，电话[唯一]，密码")
    @PostMapping("/user/register")
    public String toRegister(LoginUser loginUser, Model model){
        System.out.println("用户注册校验");
        try{
            LoginUser loginUser1 = loginUserService.findByNameAndPhone(loginUser.getName(), loginUser.getPhone());
            if(loginUser1==null){
                loginUserService.save(loginUser);
                model.addAttribute("registerMsg","注册成功");
            }else {
                model.addAttribute("registerMsg","注册用户失败，用户已存在");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            model.addAttribute("registerMsg","注册用户失败，用户已存在");
        }finally {
            return "register";
        }


    }

    @ApiOperation("负责修改等操作返回首页，需用户名，页面名")
    @GetMapping( "/user/login2")
    public String login2(Model model,String name, String pageName,HttpServletRequest request){
        System.out.println("回到明星展示页面");
        LoginUser loginUser=null;
        try{
            loginUser=loginUserService.findByName(name);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if (loginUser!=null){
            return starsController.toStars(model,0,name);
        }else {
            return pageName;
        }
    }

    @ApiOperation("负责修改密码，需用户实体类")
    @PostMapping( "/user/update")
    public String update(Model model,LoginUser loginUser){
        int id= loginUserService.findByName(loginUser.getName()).getId();
        loginUser=new LoginUser(id,loginUser.getName(),loginUser.getPhone(),loginUser.getPassword());
        try{
            synchronized (this){
                loginUserService.save(loginUser);
                model.addAttribute("updateMes","修改账号成功");
            }
        }catch (Exception e){
            model.addAttribute("updateMes","修改账号失败");
        }
        return "updateLoginUser";
    }

    @ApiOperation("跳转修改密码页面")
    @GetMapping( "/user/toUpdate")
    public String toUpdate(){
        System.out.println("进入跳转修改密码页面");
        return "updateLoginUser";
    }
}
